/*************************************************************************
	> File Name: Leetcode703.数据流中的第K大元素.c
	> Author: Maureen 
	> Mail: Maureen@qq.com 
	> Created Time: 五  9/24 17:36:50 2021
 ************************************************************************/

//利用容量为k的优先队列存储前k大元素，则队头就是第k大元素即最小的元素

//时间复杂度
// - 初始化：O(nlogk)
// - 单词插入：O(logk)
//空间复杂度：O(k)


class KthLargest {
public:
    priority_queue<int, vector<int>, greater<int>> heap;
    int k;
    KthLargest(int k, vector<int>& nums) {
        this->k = k;
        for (int num : nums) {
            add(num);
        }
    }
    
    int add(int val) {
        heap.push(val);
        if (heap.size() > k) heap.pop();
        return heap.top();
    }
};

/**
 * Your KthLargest object will be instantiated and called as such:
 * KthLargest* obj = new KthLargest(k, nums);
 * int param_1 = obj->add(val);
 */
